#include "SaviorPart.h"

// SGD Wrappers
#include "SGD Wrappers\CSGD_TextureManager.h"

// Camera access
#include "GamePlayState.h"

// Messages
#include "DestroyEntityMessage.h"
#include "MessageSystem.h"

// Cutscenes
#include "CutsceneState.h"

// Constructor
SaviorPart::SaviorPart(void) : ImageID(-1), PartNumber(SAVIOR_MAIN)
{
	ImageID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("graphics/savior.png"),D3DCOLOR_XRGB(0,255,0));
}

// Destructor
SaviorPart::~SaviorPart(void) 
{
	CSGD_TextureManager::GetInstance()->UnloadTexture(ImageID);
	ImageID = -1;
}

// Draw
void SaviorPart::Render(void)
{
	// Draw the right part
	RECT loc = {0,0,32,32};
	if( PartNumber == SAVIOR_DEPLOYMENT )
	{
		loc.top += 32;
		loc.bottom += 32;
	}
	if( PartNumber == SAVIOR_POWER )
	{
		loc.left += 32;
		loc.right += 32;
	}

	// Get the camera
	int x = GamePlayState::GetInstance()->GetCamX();
	int y = GamePlayState::GetInstance()->GetCamY();

	// Actually draw
	CSGD_TextureManager::GetInstance()->Draw(ImageID,(int)GetX() - x,(int)GetY() - y,1,1,&loc);
}

// Picking it up
bool SaviorPart::CheckCollision(IEntity* base) 
{
	if(Entity::CheckCollision(base) == true && base->GetType() == OBJ_PLAYER)
	{
		// Destroy it
		MessageSystem::GetInstance()->SendMsg(new DestroyEntityMessage(this) );

		// Prep for the next dungeon
		GamePlayState::GetInstance()->NextDungeon();

		// Run the cutscene
		CutsceneState::GetInstance()->RunCutscene(CutsceneState::SCENE_VICTORY);
		GamePlayState::GetInstance()->inCutscene = true;

		return true;
	}

	return false;
}